package me.lc.superDown.sprite;

import me.lc.superDown.core.GameSystem;

import com.wiyun.engine.actions.Animate;
import com.wiyun.engine.nodes.Animation;
import com.wiyun.engine.nodes.AtlasLabel;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.utils.ResolutionIndependent;

public class CoinBar extends Node {
	Sprite coin;
	AtlasLabel coinCount;
	static float widthHeight = ResolutionIndependent.resolveDp(22);

	public CoinBar() {
		coin = Sprite.make(GameSystem.tex_coin, frameAt(0));
		coin.setScale(1.5f);
		coin.setAnchorX(0);
		coinCount = AtlasLabel.make("0", GameSystem.tex_number,
				GameSystem.charMap);
		coinCount.setColor(WYColor3B.make(255, 255, 0));
		coinCount.setAnchorX(0);

		addChild(coin);
		addChild(coinCount);

		// 用时钟的宽度，保持一致
		coinCount.setPosition(GameSystem.tex_clock.getWidth()
				+ ResolutionIndependent.resolveDp(5), 0);
	}

	public void setCoinCount(int count) {
		coinCount.setText(count + "");
		Animation animation = (Animation) new Animation(0).autoRelease();
		animation.addFrame(0.1f, frameAt(0), frameAt(1), frameAt(2),
				frameAt(3), frameAt(4));
		Animate a = (Animate) Animate.make(animation).autoRelease();
		coin.runAction(a);
	}

	private WYRect frameAt(int i) {
		return WYRect.make(widthHeight * i, 0, widthHeight, widthHeight);
	}
}
